package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.widget.SwitchDeviceView;

import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName DeviceActBaseView
 * @Description TODO
 * @Author ShiJunqiang
 * @Date 2021/12/9 16:17
 * @Version 1.0
 */
public abstract class BaseDeviceActView extends FrameLayout {

    protected String defaultValue = "-1";
    protected String type;
    protected String deviceType;
    private Context mContext;
    private SwitchDeviceView switchView;
    public ActChangeListener mListener;
    protected DeviceBean mDevice;
    public LinkedHashMap<String, DeviceShotActBean> mDeviceActMap = new LinkedHashMap<>();

    public BaseDeviceActView(Context context) {
        this(context, null, null,null);
    }

    public BaseDeviceActView(Context context, String deviceType) {
        this(context, null, null, deviceType);
    }

    public BaseDeviceActView(Context context, String type, String deviceType) {
        this(context, null, type, deviceType);
    }

    public BaseDeviceActView(Context context, @Nullable AttributeSet attrs, String type, String deviceType) {
        this(context, attrs, 0, type, deviceType);
    }

    public BaseDeviceActView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, String type, String deviceType) {
        this(context, attrs, defStyleAttr, 0, type, deviceType);
    }

    public BaseDeviceActView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, String type, String deviceType) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.type = type;
        this.deviceType = deviceType;
        mContext = context;
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        LayoutInflater.from(mContext).inflate(getLayoutId(), this);
        initView();
    }

    public BaseDeviceActView setActMap(LinkedHashMap<String, DeviceShotActBean> deviceActMap) {
        mDeviceActMap = deviceActMap;
        updateView();
        return this;
    }

    public BaseDeviceActView setChangedListener(ActChangeListener listener) {
        mListener = listener;
        return this;
    }

    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void updateView();

    public void addAct(String key, String value) {
        mDeviceActMap.put(key, new DeviceShotActBean(type, deviceType, key, value, null));
        if (mListener != null) {
            mListener.onActChanged(mDeviceActMap);
        }
    }

    public void addAct(String key, String value, String deviceSerialNo) {
        mDeviceActMap.put(key, new DeviceShotActBean(type, deviceType, key, value, deviceSerialNo));
        if (mListener != null) {
            mListener.onActChanged(mDeviceActMap);
        }
    }

    public void addAct(String key,Map<String,Object> act) {
        mDeviceActMap.put(key, new DeviceShotActBean(type, deviceType,act));
        if (mListener != null) {
            mListener.onActChanged(mDeviceActMap);
        }
    }

    public void removeAct(String key) {
        mDeviceActMap.remove(key);
        if (mListener != null) {
            mListener.onActChanged(mDeviceActMap);
        }
    }

    public String getState(String key, String def) {
        DeviceShotActBean bean = mDeviceActMap.get(key);
        if (bean == null) return def;
        return bean.value;
    }

    public void setMusicID_view(JSONObject jsonObject){
    }
}
